package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.LogisticsSheet;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 物流单Dao
 * 作者 : zdl
 * 日期 : 2017/6/29 15:17
 */
@Repository
public class LogisticsSheetDao extends BaseDao {
    public void saveLogisticsSheet(LogisticsSheet logisticsSheet) {
        super.save(logisticsSheet);
    }

    public void deleteLogisticsSheet(LogisticsSheet logisticsSheet) {
        super.delete(logisticsSheet);
    }

    public void updateLogisticsSheet(LogisticsSheet logisticsSheet) {
        super.update(logisticsSheet);
    }

    public void saveOrUpdateLogisticsSheet(LogisticsSheet logisticsSheet) {
        super.saveOrUpdate(logisticsSheet);
    }

    public LogisticsSheet getLogisticsSheetById(String id) {
        return (LogisticsSheet) super.get(LogisticsSheet.class, id);
    }

    public List<LogisticsSheet> listAllLogisticsSheet() {
        String hql = "FROM LogisticsSheet WHERE isDelete = 0 ";
        return super.find(hql);
    }

    public Page<LogisticsSheet> pageByLogisticsSheet(Page<LogisticsSheet> page, LogisticsSheet logisticsSheet) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM LogisticsSheet ls WHERE ls.isDelete = 0 ");
        if (logisticsSheet != null) {
            Map<String, Object> param = new HashMap<>();
            String logisId = logisticsSheet.getLogisId();
            if (StringUtils.isNotBlank(logisId)) {
                hqlSb.append(" AND ls.logisId = ?1 ");
                param.put("1", logisId);
            }
            hqlSb.append(" ORDER BY ls.createTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    /**
     * 根据产品编号查询物流单信息
     *
     * @param productCode 产品编号
     * @return LogisticsSheet
     */
    public LogisticsSheet getLogisticsSheetByCode(String productCode) {
        if (StringUtils.isNotBlank(productCode)) {
            String hql = "FROM LogisticsSheet WHERE isDelete = 0 AND products LIKE '%" + productCode + "%'";
            return (LogisticsSheet) super.find(hql);
        }
        return null;
    }
}
